{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% Created by william from lexueoude.com. 更多正版技术视频讲解，公众号：1.乐学偶得 2.乐学FinTech\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([0.5, 0.5])"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import linear_model\n",
    "reg = linear_model.LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)\n",
    "reg.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])\n",
    "reg.coef_\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "\n",
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "\n",
    "# Load the diabetes dataset\n",
    "diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)\n",
    "\n",
    "# Use only one feature\n",
    "diabetes_X = diabetes_X[:, np.newaxis, 2]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "# Split the data into training/testing sets\n",
    "diabetes_X_train = diabetes_X[:-20]\n",
    "diabetes_X_test = diabetes_X[-20:]\n",
    "\n",
    "# Split the targets into training/testing sets\n",
    "diabetes_y_train = diabetes_y[:-20]\n",
    "diabetes_y_test = diabetes_y[-20:]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Coefficients: \n",
      " [938.23786125]\n",
      "Mean squared error: 2548.07\n",
      "Coefficient of determination: 0.47\n"
     ]
    }
   ],
   "source": [
    "from sklearn import linear_model\n",
    "from sklearn.metrics import mean_squared_error, r2_score\n",
    "# Create linear regression object\n",
    "regr = linear_model.LinearRegression()\n",
    "\n",
    "# Train the model using the training sets\n",
    "regr.fit(diabetes_X_train, diabetes_y_train)\n",
    "\n",
    "# Make predictions using the testing set\n",
    "diabetes_y_pred = regr.predict(diabetes_X_test)\n",
    "\n",
    "# The coefficients\n",
    "print('Coefficients: \\n', regr.coef_)\n",
    "# The mean squared error\n",
    "print('Mean squared error: %.2f'\n",
    "      % mean_squared_error(diabetes_y_test, diabetes_y_pred))\n",
    "# The coefficient of determination: 1 is perfect prediction\n",
    "print('Coefficient of determination: %.2f'\n",
    "      % r2_score(diabetes_y_test, diabetes_y_pred))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWAAAADrCAYAAABXYUzjAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAPyklEQVR4nO3dbYhdV73H8d+e9EFPrFrtmFCas7d9uFr7IDQHwSIqFBEtVsUaK6etaGGwCFqpCnrQN+XUvgiU9o0w4APtbC8SxUbji9sXQvCKSiYmkhRrLuo5p622udPYh9yTSeKcdV8s9uzzNNPZyZyz9l77+4F5MSvTsoaZ/vrPfz0FxhgBAKZvxvUEAKCsCGAAcIQABgBHCGAAcIQABgBHCGAAcOSCLF982WWXmSiKJjQVAPDTwYMHl4wxs8PjmQI4iiItLi5u3qwAoASCIGiPG6cFAQCOEMAA4AgBDACOEMAA4AgBDACOEMAAvBbHsaIo0szMjKIoUhzHrqe0KtM2NAAokjiONTc3p263K0lqt9uam5uTJNXrdZdTk0QFDMBjjUZjNXwT3W5XjUbD0YwGEcAAvNXpdDKNTxsBDMBb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bt3ioncPX5Q2gH25W7dsYZT07tvttowxq717379veMoYs+GPnTt3Gp8sLCyYMAxNEAQmDEOzsLDgekqZLCwsmEqlYiStflQqlXW/j6J/z2EYDny/yUcYhq6nBqxJ0qIZk6mFuA0N40VRNPYy9zAM1Wq1RsaHd35Ituov0vWXMzMzGvc7GwSBer2egxkBr62wt6FhbVkXEn3Y+UHvHj4hgAssaxj5sPPDl949IBHAhZY1jHyoHnlBBD4hgAssaxj5Uj3yLh58UYgALttWqyyyhBHVI5Avud8F4cPKPYByK+wuCB9W7gFgnNwHsA8r9wAwTu4D2IeVewAYJ/cB7MvKPQAMy3UAx3G82gPesmWLJLFyXxLsfEEZ5PZFjOHdDysrK6uVL+HrN14rQVnkdhta1otm4A9+9vBN4bahsfuhvPjZoyxyG8DsfigvfvYoi9wGMLsfyoufPcoitwHMvQXlxc8eZZHbRTgA8EXhFuEAwHcEMAA4QgADgCMEMAA4QgADgCOZdkEEQfC/kkbPiAIA1hMaY2aHBzMFMABg89CCAABHCGAAcIQABgBHCGAAcIQABgBHCGAAcIQABgBHCGAAcIQABgBH/h9FCo1xVgL87AAAAABJRU5ErkJggg==\n"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "# Plot outputs\n",
    "plt.scatter(diabetes_X_test, diabetes_y_test,  color='black')\n",
    "plt.plot(diabetes_X_test, diabetes_y_pred, color='blue', linewidth=1)\n",
    "\n",
    "plt.xticks(())\n",
    "plt.yticks(())\n",
    "\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "pycharm-9607c1d0",
   "language": "python",
   "display_name": "PyCharm (sklearn)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}